package com.xiaohei.rabbitmq.five;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.xiaohei.rabbitmq.utils.RabbitmqUtils;

import java.nio.charset.StandardCharsets;

/**
 * @Author: 王希辰
 * @Date: 2022/3/18 10:01
 */
public class ReceiveLogs01 {
    public static final String EXCHANGE_NAME ="logs";
    public static void main(String[] args) throws Exception {
        Channel channel= RabbitmqUtils.getChannel();
        //声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
        //声明一个队列 临时队列
        //队列名称是随机的
        //当消费者断开与对列的连接的时候 队列就自动删除
        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机与队列
        //扇出模式不管routingKey是啥都会收到消息
        channel.queueBind(queueName,EXCHANGE_NAME,"1111");
        System.out.println("等待接收消息，把接收到的消息打印在屏幕上...");
        //接收消息
        DeliverCallback deliverCallback=(consumerTag,message)->{
            System.out.println("ReceiveLogs01控制台打印接收到的消息："+new String(message.getBody(), StandardCharsets.UTF_8));
        };
        //消费者取消消息回调接口
        channel.basicConsume(queueName,true,deliverCallback,consumerTag -> {});
    }
}
